<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form ref="modalForm" :model="dataSource" status-icon>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item
            v-if="modalType === 'edit'"
            label="类型编号"
            :label-width="formLabelWidth"
          >
            <el-input v-model="dataSource.employTypeCode" disabled autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="类型名称："
            :label-width="formLabelWidth"
            prop="employTypeName"
            :rules="[
              { required: true, message: '类型名称不能为空',trigger:'blur,change'},
              { min: 3,max:10, message: '年龄长度在3-10个字符之间',trigger:'change'}
            ]"
          >
            <el-input v-model="dataSource.employTypeName" autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="备注："
        :label-width="formLabelWidth"
        prop="name"
        :rules="[
          { min: 3,max:100, message: '备注长度在3-10个字符之间',trigger:'change'}
        ]"
      >
        <el-input v-model="dataSource.memo" type="textarea" autosize autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('modalForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOneEmpType, modifyOneEmpType } from '@/api/ht/yytgl/yggl/yglxgl'
const { Message } = require('element-ui')

export default {
  name: 'Modal',
  props: {
    employeeType: {
      type: Object,
      default: () => {
        return {
          employTypeCode: '',
          id: '',
          employTypeName: '',
          creat_time: '',
          update_time: '',
          memo: ''
        }
      }
    }
  },
  data() {
    return {
      title: '员工类型信息',
      formLabelWidth: '100px', // 表单输入框字段宽度
      dialogFormVisible: false, // 对话框是否显示
      modalType: 'add',
      dataSource: { // 数据源
      }
    }
  },
  watch: {
    employeeType(newdata, prevdata) {
      this.dataSource = newdata
      this.title = '员工类型信息' + this.dataSource.id
    }
  },
  methods: {
    checkModelType(type) {
      this.modalType = type
      // 展示对话框
      this.dialogFormVisible = true
      // 判断是编辑还是新增
      if (type === 'add') {
        // 置空
        this.title = '员工类型信息'
        this.dataSource = {}
      }
    },
    handleConfirm(formName) {
      const type = this.modalType
      // 判断是编辑还是新增
      if (type === 'add') {
        // 调用新增请求
        this.addEmpTypeInfoHandle(formName)
      } else {
        // 调用修改请求
        this.modifyEmpTypeInfoHandle(formName)
      }
    },
    addEmpTypeInfoHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addEmpTypeInfo()
        } else {
          console.log('表单缺少数据或者不符合要求')
          return false
        }
      })
    },
    modifyEmpTypeInfoHandle(formName) {
      console.log(this.dataSource)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.modifyEmpTypeInfo()
        } else {
          console.log('表单缺少数据或者不符合要求')
          return false
        }
      })
    },
    async addEmpTypeInfo() {
      const res = await addOneEmpType(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async modifyEmpTypeInfo() {
      const res = await modifyOneEmpType(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    reflushDataHandle() {
      // 调用父组件方法刷新数据
      this.$emit('reflushDataHandle')
    }

  }
}
</script>

<style scoped>

</style>
